package com.heima.article.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.heima.article.mapper.ArticleConfigMapper;
import com.heima.article.mapper.ArticleContentMapper;
import com.heima.article.mapper.ArticleHomeMapper;
import com.heima.article.service.ArticleFreemarkerService;
import com.heima.article.service.ArticleHomeService;
import com.heima.article.vo.HotArticleVo;
import com.heima.common.constants.ArticleConstants;
import com.heima.model.article.dtos.ArticleDto;
import com.heima.model.article.dtos.ArticleHomeDto;
import com.heima.model.article.pojos.ApArticle;
import com.heima.model.article.pojos.ApArticleConfig;
import com.heima.model.article.pojos.ApArticleContent;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @author DiXiaoNie
 * @since 2022/12/19 12:18
 */
@Service
public class ArticleHomeServiceImpl implements ArticleHomeService {

    @Resource
    private ArticleHomeMapper articleHomeMapper;

    @Resource
    private ArticleConfigMapper articleConfigMapper;

    @Resource
    private ArticleContentMapper articleContentMapper;

    @Autowired
    private ArticleFreemarkerService articleFreemarkerService;

    /**
     * 保存
     *
     * @param dto dto
     * @return {@link ResponseResult }
     * @author DiXiaoNie
     * @date 2022/12/22
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseResult save(ArticleDto dto) {
        //1.参数校验
        if (dto == null || StringUtils.isBlank(dto.getContent())) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        ApArticle apArticle = new ApArticle();
        BeanUtils.copyProperties(dto, apArticle);
        apArticle.setLikes(0);
        apArticle.setViews(0);
        apArticle.setCollection(0);
        apArticle.setComment(0);
        //2.判断是否是保存还是修改
        if (dto.getId() == null) {
            //3.如果是保存文章
            //保存ap_article

            this.articleHomeMapper.insert(apArticle);
            //保存ap_article_config
            ApArticleConfig apArticleConfig = new ApArticleConfig(apArticle.getId());
            this.articleConfigMapper.insert(apArticleConfig);
            //保存ap_article_content
            ApArticleContent apArticleContent = new ApArticleContent();
            apArticleContent.setContent(dto.getContent());
            apArticleContent.setArticleId(apArticle.getId());
            this.articleContentMapper.insert(apArticleContent);
        } else {
            //4.如果是修改文章
            //修改文章ap_article
            this.articleHomeMapper.updateById(apArticle);
            //修改ap_article_content
            ApArticleContent apArticleContent = this.articleContentMapper.selectOne(Wrappers.<ApArticleContent>lambdaQuery().eq(ApArticleContent::getArticleId, apArticle.getId()));
            if (apArticleContent != null) {
                apArticleContent.setContent(dto.getContent());
                this.articleContentMapper.updateById(apArticleContent);
            }
        }

        this.articleFreemarkerService.buildArticleToMinio(apArticle, dto.getContent());

        ResponseResult responseResult = ResponseResult.okResult(apArticle == null ? "" : apArticle.getId());
        return responseResult;
    }

    /**
     * 从redis中获取热点数据
     *
     * @param dto  dto
     * @param type 类型
     * @return {@link ResponseResult }
     * @author DiXiaoNie
     * @date 2022/12/22
     */
    @Autowired
    private StringRedisTemplate redisTemplate;

    /**
     * 从redis中获取文章
     *
     * @param dto dto
     * @return {@link ResponseResult }
     * @author DiXiaoNie
     * @date 2023/01/06
     */
    @Override
    public ResponseResult loadV2(ArticleHomeDto dto) {
        //1.声明redis的key

        String key = ArticleConstants.HOT_ARTICLE_FIRST_PAGE + dto.getTag();

        String hotArticleVoListStr = redisTemplate.opsForValue().get(key);
        List<HotArticleVo> hotArticleVoList = JSONArray.parseArray(hotArticleVoListStr, HotArticleVo.class);
        return ResponseResult.okResult(hotArticleVoList);
    }
    @Override
    public ResponseResult load(ArticleHomeDto dto, short type) {
        //1.参数校验
        if (StringUtils.isBlank(dto.getTag())) {
            dto.setTag(ArticleConstants.DEFAULT_TAG);
        }
        if (dto.getSize() == null) {
            dto.setSize(10);
        }
        if (dto.getMaxBehotTime() == null) {
            dto.setMaxBehotTime(new Date());
        }

        if (dto.getMinBehotTime() == null) {
            dto.setMinBehotTime(new Date());
        }

        //2.调用mapper接口
        List<ApArticle> apArticleList = this.articleHomeMapper.loadArticleList(dto, type);

        //3.返回数据
        return ResponseResult.okResult(apArticleList);
    }
}
